﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DLL;
using Core;
using System.Drawing.Imaging;
using Logging;

namespace QuanLyBanLe
{
    public partial class FormAddProduct : Form
    {
        private ProducerService m_ProducerService;
        private ProductTypeService m_ProductTypeService;
        private ProductService m_ProductService;
        private IList<Producer> m_Producers;
        private IList<ProductType> m_ProductTypes;
        private IList<Product> m_Products;
        private bool m_LoadComplete;
        public bool Saved = false;
        private Product m_Product;

        public FormAddProduct()
        {
            m_ProducerService = new ProducerService();
            m_ProductTypeService = new ProductTypeService();
            m_ProductService = new ProductService();
            InitializeComponent();
        }

        private void SetProductType()
        {
            m_ProductTypes = m_ProductTypeService.GetProductTypes();
            cboProductType.DataSource = m_ProductTypes;
        }

        private void SetProducer()
        {
            m_Producers = m_ProducerService.GetProducers();
            cboProducer.DataSource = m_Producers;
        }

        private void SetAutoCompleteForCombobox()
        {
            cboProductType.DropDownStyle = ComboBoxStyle.DropDown;
            cboProductType.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboProductType.AutoCompleteSource = AutoCompleteSource.ListItems;
            cboProducer.DropDownStyle = ComboBoxStyle.DropDown;
            cboProducer.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboProducer.AutoCompleteSource = AutoCompleteSource.ListItems;
        }

        private void FormAddProduct_Load(object sender, EventArgs e)
        {
            SetProductType();
            SetProducer();
            SetAutoCompleteForCombobox();
            this.Text += FormMain.GetShopInfo();
            m_LoadComplete = true;
        }

        private bool ValidateData()
        {
            bool isError = false;
            if (cboProducer.SelectedValue == null)
            {
                isError = true;
                errorProvider.SetError(cboProducer, "Vui lòng điền thông tin");
                return isError;
            }
            else
            {
                errorProvider.SetError(cboProducer, "");
            }

            if (cboProductType.SelectedValue == null)
            {
                isError = true;
                errorProvider.SetError(cboProductType, "Vui lòng điền thông tin");
                return isError;
            }
            else
            {
                errorProvider.SetError(cboProductType, "");
            }

            if (string.IsNullOrEmpty(tbProductName.Text))
            {
                isError = true;
                errorProvider.SetError(tbProductName, "Vui lòng điền thông tin");
                return isError;
            }
            else
            {
                errorProvider.SetError(tbProductName, "");
            }
            return isError;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool hasError = ValidateData();
                if (!hasError)
                {
                    if (Saved == false)
                    {
                        m_Product = new Product()
                        {
                            Id = 0,
                            IsDeleted = false,
                            ProductTypeId = Convert.ToInt32(cboProductType.SelectedValue),
                            ProducerId = Convert.ToInt32(cboProducer.SelectedValue),
                            Amount = 0,
                            Name = tbProductName.Text,
                            AddedBy = FormMain.s_User.Id,
                            AddedDate = DateTime.Now
                        };
                        m_Product.SalePrice = Convert.ToInt32(tbSalePrice.Text);
                        m_ProductService.AddProduct(m_Product);
                        // update barcode
                        string code = "";
                        BarCode.CreateBarCode(m_Product.Id.ToString(), m_Product.Name, m_Product.SalePrice, m_Product.SalePrice, ref code, FormMain.s_OS);
                        code = code.Substring(1);
                        m_Product.Barcode = code;
                        m_ProductService.UpdateProduct(m_Product);
                        Saved = true;
                    }
                    else
                    {
                        m_Product.ProducerId = Convert.ToInt32(cboProducer.SelectedValue);
                        m_Product.ProductTypeId = Convert.ToInt32(cboProductType.SelectedValue);
                        m_Product.Name = tbProductName.Text;
                        m_Product.SalePrice = Convert.ToInt32(tbSalePrice.Text);
                        m_ProductService.UpdateProduct(m_Product);
                    }
                    // TODO: Switch to edit mode
                }
            }
            catch (Exception ex)
            {
                MyLogger.LogException("Error in FormAddProduct : ", ex);
                MessageBox.Show("Có lỗi xảy ra, không thêm được sản phẩm");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbo_KeyDown(object sender, KeyEventArgs e)
        {
            ComboBox cbo = sender as ComboBox;
            if (cbo.DroppedDown)
            {
                cbo.DroppedDown = false;
            }
        }
    }
}
